package com.stone.serverless.beetl.loader;

import cn.hutool.core.util.ObjectUtil;
import org.beetl.sql.clazz.kit.ClassLoaderKit;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: TODO
 * @Author: MECHREVO
 * @Date: 2022/4/6 10:08
 * @Version: 1.0.0
 */
public class SeClassLoaderKit extends ClassLoaderKit {
    List<ClassLoader> otherLoaders = new ArrayList();
    ThreadLocal<ClassLoader> threadLocalClassLoader = new ThreadLocal<>();


    public SeClassLoaderKit(ClassLoader first) {
        super(first);
        this.otherLoaders.add(first);
    }

    public void setClassLoader(ClassLoader c) {
        this.otherLoaders.add(c);
        threadLocalClassLoader.set(c);
    }

    public ClassLoader getPreferredLoader() {
        ClassLoader classLoader = threadLocalClassLoader.get();
        if (ObjectUtil.isNull(classLoader)) {
            return super.getPreferredLoader();
        }
        return classLoader;
    }

}
